#ifndef IDGENERATOR_H
#define IDGENERATOR_H

#include <cstdint>
#include <chrono>

typedef uint64_t ID;

class IDGenerator
{
public:
    IDGenerator() = delete;
    IDGenerator(uint8_t mechineID);

    inline ID generate() {
        ID res = 0;
        res |= (uint64_t)mechineID << 56;
        uint64_t timePoint = std::chrono::system_clock::now().time_since_epoch().count() / 1000;
        timePoint &= 0xFFFFF;
        res |= timePoint << 16;
        if (timePoint != lastTime)
            count = 0;
        lastTime = timePoint;
        count++;
        res |= count;
        return res;
    }

private:
    uint8_t mechineID;
    uint64_t lastTime = 0;
    uint16_t count = 0;
};

#endif // IDGENERATOR_H
